<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets" 
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title></title>
    </h:head>
    <h:body>

        <ui:composition template="../../templates/master.xhtml">
            <ui:define name="content">
                <p:dataTable value="#{accountsController.accounts}" paginator="true" rows="10"
                             paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}" 
                             var="ac" rowsPerPageTemplate="5,10,15" id="accTable" lazy="true">
                    <p:column styleClass="row-item-#{ac.id}" headerText="ID">
                        <h:outputText value="#{ac.id}"/>
                    </p:column>
                    <p:column sortBy="#{ac.username}" filterBy="#{ac.username}" headerText="Name">
                        <h:outputText value="#{ac.username}"/>
                    </p:column>
                    <p:column headerText="Department">
                        <h:outputText value="#{ac.departmentID.name}"/>
                    </p:column>
                    <p:column headerText="Action">
                        <p:commandButton oncomplete="acDialog.show()" update=":form:catDetial" id="ViewDetailButton" title="View" icon="ui-icon-search">
                            <f:setPropertyActionListener value="#{ac}" target="#{accountsController.selected}"/>
                        </p:commandButton>
                        <p:commandButton id="EditButton" ajax="false" action="#{accountsController.prepareEdit()}" title="Edit" icon="ui-icon-pencil">
                            <f:setPropertyActionListener value="#{ac}" target="#{accountsController.selected}"/>
                        </p:commandButton>
                        <p:commandButton  update="accTable" oncomplete="onDeleteRowComplete(xhr,status,args)" id="DeleteButton" title="Delete" action="#{accountsController.destroy()}" icon="ui-icon-close">
                            <f:setPropertyActionListener value="#{ac}" target="#{accountsController.selected}"/>
                        </p:commandButton>
                    </p:column>
                </p:dataTable>
                <p:commandButton value="Create" action="#{departmentsController.prepareCreate()}"/>
                <p:dialog header="Account Detail" widgetVar="acDialog" resizable="false" id="acDlg"  
                          showEffect="fade" hideEffect="explode" modal="true">  
                    <h:panelGrid id="catDetial" columns="2" cellpadding="4" style="margin:0 auto;">  
                        <f:facet name="header">  
                        </f:facet>  
                        <h:outputText value="FirstName:"/>
                        <h:outputText value="#{accountsController.selected.firstname}" style="font-weight: bold"/>    
                        <h:outputText value="Last Name:"/>
                        <h:outputText value="#{accountsController.selected.lastname}" style="font-weight: bold"/>    
                        <h:outputText value="Email:"/>
                        <h:outputText value="#{accountsController.selected.email}" style="font-weight: bold"/>    
                        <h:outputText value="Birthday:"/>
                        <h:outputText value="#{accountsController.selected.birthday}" style="font-weight: bold">
                            <f:convertDateTime pattern="dd/MM/yyyy"></f:convertDateTime>
                        </h:outputText>    
                        <h:outputText value="DateCreate:"/>
                        <h:outputText value="#{accountsController.selected.dateCreated}" style="font-weight: bold">
                            <f:convertDateTime pattern="dd/MM/yyyy"></f:convertDateTime>
                        </h:outputText> 

                        <h:outputText value="Last Update:" />  
                        <h:outputText value="#{accountsController.selected.lastUpdate}" style="font-weight: bold">
                            <f:convertDateTime pattern="dd/MM/yyyy"></f:convertDateTime></h:outputText>
                        <h:outputText value="Role:" />  
                        <h:outputText value="#{accountsController.selected.roles}" style="font-weight:bold"/>
                        <h:outputText value="Department:" />  
                        <h:outputText value="#{accountsController.selected.departmentID.name}" style="font-weight: bold"/>
                    </h:panelGrid>  

                </p:dialog>  
            </ui:define>
            <ui:define name="footer">
                <ui:include src="../../templates/footer.xhtml"/>
                <script type="text/javascript">
                    function onDeleteRowComplete(xhr, status,args){
                        var item = args.item;
                        $("td.row-item-"+item).closest("tr").fadeOut("fast").remove();
                        console.log(item);
                    }
                </script>
            </ui:define>
        </ui:composition>
    </h:body>
</html>

